package vip.liux.contracts.models.files;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Getter;
import lombok.Setter;
import vip.liux.domain.entities.auditing.AuditedAggregateRoot;

import java.util.LinkedHashSet;
import java.util.Set;

@Getter
@Setter
@Entity
@Table(name = "fm_directory_descriptors")
public class DirectoryDescriptor extends AuditedAggregateRoot<Long, DirectoryDescriptor> {
    @Column(name = "tenant_id")
    private String tenantId;

    @Size(max = 255)
    @NotNull
    @Column(name = "name", nullable = false)
    private String name;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "parent_id")
    private DirectoryDescriptor parent;

    @OneToMany(mappedBy = "directory")
    private Set<FileDescriptor> files = new LinkedHashSet<>();

    public DirectoryDescriptor() {
        super();
    }

    public DirectoryDescriptor(String name, DirectoryDescriptor parent, String tenantId) {
        this.tenantId = tenantId;
        this.parent = parent; // this is a bug
        setName(name);
    }

    protected void setName(String name) {
        this.name = FileNameValidator.checkDirectoryName(name);
    }

    public Long getParentId() {
        return parent == null ? null : parent.getId();
    }
}